package com.blrs.service.teacherInfo.impl;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.common.utils.uuid.UUID;
import com.blrs.domain.TeacherInfoNew;
import com.blrs.mapper.TeacherInfoNewMapper;
import com.blrs.service.teacherInfo.ITeacherInfoNewService;
import com.blrs.system.mapper.SysUploadFileMapper;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TeacherInfoNewServiceImpl implements ITeacherInfoNewService {
    
    @Autowired
    private TeacherInfoNewMapper teacherInfoMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;

    @Override
    public TeacherInfoNew selectTeacherInfoById(String id) {
        TeacherInfoNew teacherInfoNew = teacherInfoMapper.selectTeacherInfoById(id);
        String s = sysUploadFileMapper.selectUrlByCode("teacherInfo",teacherInfoNew.getUploadFile());
        if (s!= null){
            teacherInfoNew.setFileUrl(s);
        }
        return teacherInfoNew;

    }

    @Override
    public List<TeacherInfoNew> selectTeacherInfoList(TeacherInfoNew teacherInfo) {
        teacherInfo.setOrganizationId(SecurityUtils.getOrgId());
        return teacherInfoMapper.selectTeacherInfoList(teacherInfo);
    }

    @Override
    public int insertTeacherInfo(TeacherInfoNew teacherInfo) {
        teacherInfo.setId(UUID.randomUUID().toString());
        teacherInfo.setOrganizationId(SecurityUtils.getOrgId());
        teacherInfo.setOrganizationName(SecurityUtils.getOrgName());
        teacherInfo.setCreateBy(SecurityUtils.getUsername());
        teacherInfo.setCreateTime(new Date());
        return teacherInfoMapper.insertTeacherInfo(teacherInfo);
    }

    @Override
    public int updateTeacherInfo(TeacherInfoNew teacherInfo) {
        teacherInfo.setUpdateBy(SecurityUtils.getUsername());
        teacherInfo.setUpdateTime(new Date());
        return teacherInfoMapper.updateTeacherInfo(teacherInfo);
    }

    @Override
    public int deleteTeacherInfoByIds(String[] ids) {
        return teacherInfoMapper.deleteTeacherInfoByIds(ids);
    }

    @Override
    public int deleteTeacherInfoById(String id) {
        return teacherInfoMapper.deleteTeacherInfoById(id);
    }

    @Override
    public String importTeacherInfo(List<TeacherInfoNew> teacherInfoList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(teacherInfoList) || teacherInfoList.size() == 0) {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        
        for (TeacherInfoNew teacherInfo : teacherInfoList) {
            try {
                teacherInfo.setCreateBy(operName);
                teacherInfo.setOrganizationId(SecurityUtils.getOrgId());
                teacherInfo.setOrganizationName(SecurityUtils.getOrgName());
                this.insertTeacherInfo(teacherInfo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、姓名 " + teacherInfo.getName() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、姓名 " + teacherInfo.getName() + " 导入失败：" + e.getMessage());
            }
        }
        
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}